<?php
class BannersController extends AppController
{
	
	var $name = 'Banners';
	var $uses = array('Banner');
	var $helpers = array('Html', 'Form', 'Javascript', 'Tinymce');
	var $components = array('Upload');
	var $layout = 'admin';
	
	
	function index()
	{	
		$this->paginate = array('limit' => 15, 'page' => 1);
		$this->set ('banners', $this->paginate ('Banner'));
	}
	
	function add()
	{
		if(! empty ($this->data))
		{
			$this->Banner->create ();
			if($this->Upload->upload ($this->data['Banner']['image'], WWW_ROOT . 'img' . DS))
			{
				$this->data['Banner']['images'] = $this->data['Banner']['image']['name'];
			}
			if($this->Banner->save ($this->data))
			{
				$this->Session->setFlash (__ ('đã được thêm', true));
				$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('Không thêm được', true));
			}
		}
	}
	
	function edit($id = null)
	{
		$banners = $this->Banner->read (null, $id);
		if(! $id && empty ($this->data))
		{
			$this->Session->setFlash (__ ('Invalid id', true));
			$this->redirect (array('action' => 'index'));
		}
		if(! empty ($this->data))
		{
			if($this->data['Banner']['image']['name'])
			{
				if(! $this->Upload->upload ($this->data['Banner']['image'], WWW_ROOT . 'img' . DS))
				{
					$this->Session->setFlash (__ ('Không upload được ảnh', true));
					$this->redirect (array('action' => 'edit/' . $id));
				}
				@unlink (WWW_ROOT . 'img' . DS . $banners['Banner']['images']);
				$this->data['Banner']['images'] = $this->data['Banner']['image']['name'];
			}
			if($this->Banner->save ($this->data))
			{
				$this->Session->setFlash (__ ('Sửa thành công', true));
				$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('Không sửa được', true));
			}
		}
		
		if(empty ($this->data))
		{
			$this->data = $banners;
		}
	}
}
?>